<?php
/**
 * @author yuanyitian
 * @date 2024/10/20 23:27
 */

namespace api\admin;

use utils\base64_to_file;
use utils\oss;

class setting
{

    //获取初始化数据
    public function getInit()
    {
        $con = con();
        $date_0 = date('Y-m-d');
        $time_0 = strtotime($date_0);
        $data = mysqli_query($con, "select * from setting order by id ASC");
        $data = mysqli_fetch_all($data, true);
        if (count($data) < 3) {
            mysqli_close($con);
            return out_error('缺少初始化数据！请联系开发！');
        }

        $start_time_0 = date('H:i', $time_0 + $data[9]['value_1']);
        $start_time_1 = date('H:i', $time_0 + $data[0]['value_1']);
        $start_time_2 = date('H:i', $time_0 + $data[1]['value_1']);
        $start_time_3 = date('H:i', $time_0 + $data[2]['value_1']);
        $end_time_0 = date('H:i', $time_0 + $data[9]['value_2']);
        $end_time_1 = date('H:i', $time_0 + $data[0]['value_2']);
        $end_time_2 = date('H:i', $time_0 + $data[1]['value_2']);
        $end_time_3 = date('H:i', $time_0 + $data[2]['value_2']);
        $send_status=(bool)$data[5]['value_1'];
        $send_price=$data[6]['value_1'];
        $print_id=$data[7]['value_1'];
        $light=$data[8]['value_1'];
        $fix_price_1=$data[11]['value_1'];
        $fix_price_2=$data[12]['value_1'];
        $fix_price_3=$data[13]['value_1'];
        $area_1_open=(bool)$data[14]['value_1'];
        $area_1_title=$data[15]['value_1'];
        $area_2_open=(bool)$data[16]['value_1'];
        $area_2_title=$data[17]['value_1'];

        $power=json_decode($data[10]['value_1'],true);


        $swiper = $data[3]['value_1'];
        if (empty($swiper)) {
            $swiper = [];
        } else {
            $swiper = explode(',', $swiper);
        }

        $background = $data[4]['value_1'];
        if (empty($background)) {
            $background = '';
        }


        mysqli_close($con);
        return out_right([
            'setting_time' => [
                'start_time_0' => $start_time_0,
                'start_time_1' => $start_time_1,
                'start_time_2' => $start_time_2,
                'start_time_3' => $start_time_3,
                'end_time_0' => $end_time_0,
                'end_time_1' => $end_time_1,
                'end_time_2' => $end_time_2,
                'end_time_3' => $end_time_3
            ],
            'swiper' => $swiper,
            'background' => $background,
            'send_status'=>$send_status,
            'send_price'=>$send_price,
            'print_id'=>$print_id,
            'light'=>$light,
            'power'=>$power,
            'fix_price_1'=>$fix_price_1,
            'fix_price_2'=>$fix_price_2,
            'fix_price_3'=>$fix_price_3,
            'area'=>[
                'area_1_open'=>$area_1_open,
                'area_1_title'=>$area_1_title,
                'area_2_open'=>$area_2_open,
                'area_2_title'=>$area_2_title
            ]
        ]);
    }

    //保存配送打包价格
    public function saveSendPrice($data){
        $price=$data['send_price'];
        $con = con();
        $res=mysqli_query($con,"update setting set value_1='$price' where id=7");
        mysqli_close($con);
        if($res){
            return out_right('保存成功！');
        }else{
            return out_error('保存失败！');
        }
    }

    //保存自助餐时段设置
    public function saveTime($data)
    {
        $start_time_1 = $data['start_time_1'];
        $start_time_2 = $data['start_time_2'];
        $start_time_3 = $data['start_time_3'];
        $end_time_1 = $data['end_time_1'];
        $end_time_2 = $data['end_time_2'];
        $end_time_3 = $data['end_time_3'];

        $con = con();
        $date_0 = date('Y-m-d');
        $time_0 = strtotime($date_0);

        $start_time_1 = strtotime($date_0 . ' ' . $start_time_1) - $time_0;
        $start_time_2 = strtotime($date_0 . ' ' . $start_time_2) - $time_0;
        $start_time_3 = strtotime($date_0 . ' ' . $start_time_3) - $time_0;
        $end_time_1 = strtotime($date_0 . ' ' . $end_time_1) - $time_0;
        $end_time_2 = strtotime($date_0 . ' ' . $end_time_2) - $time_0;
        $end_time_3 = strtotime($date_0 . ' ' . $end_time_3) - $time_0;

        mysqli_query($con, "update setting set value_1=$start_time_1,value_2=$end_time_1 where id=1");
        mysqli_query($con, "update setting set value_1=$start_time_2,value_2=$end_time_2 where id=2");
        $res = mysqli_query($con, "update setting set value_1=$start_time_3,value_2=$end_time_3 where id=3");

        mysqli_close($con);
        if ($res) {
            return out_right('保存成功！');
        } else {
            return out_error('保存失败！');
        }

    }


    //保存预订时段设置
    public function savePreTime($data)
    {
        $start_time_0 = $data['start_time_0'];
        $end_time_0 = $data['end_time_0'];

        $con = con();
        $date_0 = date('Y-m-d');
        $time_0 = strtotime($date_0);

        $start_time_0 = strtotime($date_0 . ' ' . $start_time_0) - $time_0;
        $end_time_0 = strtotime($date_0 . ' ' . $end_time_0) - $time_0;

        $res = mysqli_query($con, "update setting set value_1=$start_time_0,value_2=$end_time_0 where id=10");

        mysqli_close($con);
        if ($res) {
            return out_right('保存成功！');
        } else {
            return out_error('保存失败！');
        }

    }

    //保存配送设置
    public function saveSend($data){
        $status=(int)$data['status'];
        $con=con();
        $res=mysqli_query($con,"update setting set value_1=$status where id=6");
        mysqli_close($con);
        if($res){
            return out_right('保存成功！');
        }else{
            return out_error('保存失败！');
        }
    }

    //保存轮播图
    public function saveImg($data)
    {
        $con = con();
        $img = $data['img'];
        $date = date('Y-m-d');
        $url = [];
        for ($i = 0; $i < count($img); $i++) {
            $file = base64_to_file::get("", $img[$i]["file_name"], $img[$i]["file"]);
            $res = oss::upload("swiper/$date/" . $file["name"], $file["path"]);
            $url[] = $res["info"]["url"];
        }
        $url = implode(',', $url);
        $res = mysqli_query($con, "update setting set value_1='$url' where id=4");
        mysqli_close($con);
        if ($res) {
            return out_right('保存成功！');
        } else {
            return out_error('保存失败！');
        }
    }

    //保存首页背景图
    public function saveBackImg($data)
    {
        $con = con();
        $date = date('Y-m-d');
        $file = base64_to_file::get("", $data["file_name"], $data["file"]);
        $res = oss::upload("background/$date/" . $file["name"], $file["path"]);
        $url = $res["info"]["url"];
        $res = mysqli_query($con, "update setting set value_1='$url' where id=5");
        mysqli_close($con);
        if ($res) {
            return out_right('保存成功！');
        } else {
            return out_error('保存失败！');
        }
    }

    //保存打印机设置
    public function savePrint($data){
        $print_id=$data['print_id'];
        $con = con();
        $res=mysqli_query($con,"update setting set value_1='$print_id' where id=8");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    //保存跑马灯内容
    public function saveLight($data){
        $content=$data['content'];
        $con = con();
        $res=mysqli_query($con,"update setting set value_1='$content' where id=9");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    /**
     * 保存堂食收款码价格
     */
    public function saveFixPrice($data){
        $fix_price_1=$data['fix_price_1'];
        $fix_price_2=$data['fix_price_2'];
        $fix_price_3=$data['fix_price_3'];
        $con=con();
        mysqli_query($con,"update setting set value_1='$fix_price_1' where id=12");
        mysqli_query($con,"update setting set value_1='$fix_price_2' where id=13");
        mysqli_query($con,"update setting set value_1='$fix_price_3' where id=14");
        $num=mysqli_affected_rows($con);
        mysqli_close($con);

        return out_right();
    }

    /**
     * 保存自定义区域设置
     */
    public function saveArea($data){
        $area_1_open=(int)$data['area_1_open'];
        $area_1_title=$data['area_1_title'];
        $area_2_open=(int)$data['area_2_open'];
        $area_2_title=$data['area_2_title'];

        $con=con();
        mysqli_query($con,"update setting set value_1='$area_1_open' where id=15");
        mysqli_query($con,"update setting set value_1='$area_1_title' where id=16");
        mysqli_query($con,"update setting set value_1='$area_2_open' where id=17");
        mysqli_query($con,"update setting set value_1='$area_2_title' where id=18");
        $num=mysqli_affected_rows($con);
        mysqli_close($con);

        return out_right();
    }

}